Skip to content

Method: mapAssertions(AxiomDescriptor, Map, Map)

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.jena;
14:
15: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
16: import cz.cvut.kbss.ontodriver.jena.connector.InferredStorageConnector;
17: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
18: import cz.cvut.kbss.ontodriver.jena.util.JenaUtils;
19: import cz.cvut.kbss.ontodriver.model.Assertion;
20: import cz.cvut.kbss.ontodriver.model.Axiom;
21: import org.apache.jena.rdf.model.Property;
22: import org.apache.jena.rdf.model.RDFNode;
23: import org.apache.jena.rdf.model.Resource;
24: import org.apache.jena.rdf.model.ResourceFactory;
25:
26: import java.net.URI;
27: import java.util.Collection;
28: import java.util.HashMap;
29: import java.util.Map;
30: import java.util.Set;
31:
32: class MainAxiomLoader {
33:
34: private final AbstractAxiomLoader inferredLoader;
35: private final ExplicitAxiomLoader explicitLoader;
36:
37: MainAxiomLoader(StorageConnector connector, InferredStorageConnector inferredConnector) {
38: this.explicitLoader = new ExplicitAxiomLoader(connector);
39: // It is possible that the inferred connector is null - if we are using the read_committed strategy or only snapshot,
40: // without inference
41: this.inferredLoader = new InferredAxiomLoader(inferredConnector);
42: }
43:
44: /**
45: * Checks whether the storage contains the specified axiom.
46: *
47: * @param axiom Axiom whose existence should be verified
48: * @param contexts Contexts to search, optional (empty indicates the default context)
49: * @return {@code true} if the axiom exists, {@code false} otherwise
50: */
51: boolean contains(Axiom<?> axiom, Set<URI> contexts) {
52: return axiom.getAssertion().isInferred() ? inferredLoader.contains(axiom, contexts) :
53: explicitLoader.contains(axiom, contexts);
54: }
55:
56: /**
57: * Checks whether the storage inferred the specified axiom.
58: * <p>
59: * Note that given the nature of the Jena API, this method will return {@code false} even if the statement is both
60: * asserted and inferred, as there is no way to easily ask only for inferred statements but both asserted and
61: * inferred statements are returned.
62: * <p>
63: * Also note that if the repository does not contain the statement at all, {@code false} is returned.
64: *
65: * @param axiom Axiom whose inference to check
66: * @param contexts Contexts to search, optional (empty indicates the default context)
67: * @return iff the specified statement is inferred in any of the specified contexts
68: */
69: boolean isInferred(Axiom<?> axiom, Set<URI> contexts) {
70: final Resource subject = ResourceFactory.createResource(axiom.getSubject().getIdentifier().toString());
71: final Property property = ResourceFactory.createProperty(axiom.getAssertion().getIdentifier().toString());
72: final RDFNode object = JenaUtils.valueToRdfNode(axiom.getAssertion(), axiom.getValue());
73: return inferredLoader.contains(subject, property, object, contexts) && !explicitLoader.contains(subject, property, object, contexts);
74: }
75:
76: /**
77: * Loads axioms corresponding to the specified descriptor.
78: *
79: * @param descriptor Descriptor of axioms to load
80: * @return Matching axioms
81: */
82: Collection<Axiom<?>> find(AxiomDescriptor descriptor) {
83: final Map<String, Assertion> asserted = new HashMap<>(descriptor.getAssertions().size());
84: final Map<String, Assertion> inferred = new HashMap<>(descriptor.getAssertions().size());
85: mapAssertions(descriptor, asserted, inferred);
86: final Collection<Axiom<?>> result = explicitLoader.find(descriptor, asserted);
87: result.addAll(inferredLoader.find(descriptor, inferred));
88: return result;
89: }
90:
91: private static void mapAssertions(AxiomDescriptor descriptor, Map<String, Assertion> asserted,
92: Map<String, Assertion> inferred) {
93:• for (Assertion a : descriptor.getAssertions()) {
94:• if (a.isInferred()) {
95: inferred.put(a.getIdentifier().toString(), a);
96: } else {
97: asserted.put(a.getIdentifier().toString(), a);
98: }
99: }
100: }
101: }